<?php


	namespace app\admin\controller\scheduling;


	use app\admin\model\scheduling\SchedulingModel;
	use core\basic\Controller;

	class SchedulingController extends Controller
	{
		protected $model;

		public function __construct()
		{
			$this->model = new SchedulingModel();

		}

		public function index()
		{
			if ($_POST) {
				$data = array(
					'Scheduling_name' => post('Scheduling_name'),
					'Scheduling_amount' => post('Scheduling_amount'),
					'Scheduling_state' => post('Scheduling_state'),
					'Scheduling_group' => post('Scheduling_group'),
					'Scheduling_StartTime' => post('Scheduling_StartTime'),
					'Scheduling_EndTime' => post('Scheduling_EndTime'),
					'Scheduling_current_amount' => post('Scheduling_amount'),
					'number' => post('number'),
					'chance' => post('chance'),
					'minimum' => post('minimum'),
					'largest' => post('largest'),

				);
				if ($this->model->checkScheduling()) {
					if ($this->model->modScheduling($data)) {
						$this->log('修改排单成功！');
						success('修改成功！', -1);
					} else {
						location(-1);
					}
				} else {

					if ($this->model->addScheduling($data)) {
						$this->log('修改排单成功！');
						success('修改成功！', -1);
					} else {
						location(-1);
					}
				}
			}
			$this->assign('list', true);
			$this->assign('groups', model('admin.member.MemberGroup')->getSelect());

			$this->assign('profit', $this->model->profitList());
			$this->assign('Scheduling', $this->model->getList());
			$this->display('scheduling/scheduling.html');
		}

		public function schedulinglist()
		{


				if (!!($field = get('field', 'var')) && !!($keyword = get('keyword', 'vars'))) {
					$res = $this->model->findUserPoolList($field, $keyword);
				} else {
					$res = $this->model->UserPoolList();
				}
				$this->assign('PoolList', $res);
			$this->display('scheduling/list.html');
		}

		public function schedulinglistJson()
		{
			if ($_GET) {

				$count = $this->model->count();
				$res = $this->model->PoolList();
				$data = array(
					'code' => 0,
					'msg' => '',
					'count' => $count,
					'data' => $res
				);
				return json_encode($data);
			}


		}

		public function del()
		{
			if ($_GET) {
				if (!$id = get('id', 'int')) {
					json(0, '删除失败');
				}

				if ($this->model->PoolInfo(['id' => $id])->status == 1) {
					json(0, '当前订单为结束,无法删除！');
				}
				if ($this->model->delPool($id)) {
					$this->log('删除订单' . $id . '成功！');
					json(1, '删除成功');
				} else {
					$this->log('删除订单' . $id . '失败！');
					json(0, '删除失败');
				}
			}
		}

		public function SchedulingAdd()
		{

			if ($_POST) {
				$data = array(
					'pname' => post('pname'),
					'qauota' => post('qauota'),
					'sauota' => post('qauota'),
					'begin_time' => post('begin_time'),
					'end_time' => post('end_time'),
					'status' => post('status'),
					'add_time' => get_datetime(),
				);

				if (!$data['begin_time']) {
					error('请选择开始时间！', -1);
				}
				if (strtotime($data['begin_time']) >= strtotime($data['end_time'])) {
					error('时间选择错误！', -1);
				}
				if (strtotime($data['begin_time']) < strtotime(get_datetime())) {
					error('开始时间不能大于当前时间', -1);
				}
				if (!$data['end_time']) {
					error('请选择结束时间！', -1);
				}
				//检查是否有开启的订单池
				if ($this->model->NewPool(['status' => 1])) {
					error('当前有正在进行的排单！', -1);
				}
				// 执行添加
				if ($this->model->SchedulingAdd($data)) {
					$this->log('添加订单池成功！');
					if (!!$backurl = get('backurl')) {
						success('添加订单池成功！', base64_decode($backurl));
					} else {
						success('添加订单池成功！', url('/admin/Scheduling/schedulinglist'));
					}
				} else {
					$this->log('添加订单池失败！');
					error('新增失败！', -1);
				}
			}

		}


		public function single_profit()
		{
			if ($_POST) {

				$buy_days = post('buy_days');//排单天数
				$status = post('status');//排单天数
				$Settlement_profits = post('Settlement_profits');//结算利润
				$Minimum_amount = post('Minimum_amount');//最小投资额

				if ($Settlement_profits == 0) {
					error('结算利润不正确！', -1);
				}
				if ($buy_days == 0) {
					error('排单天数不正确！', -1);
				}
				if (!$buy_days) {
					error('排单天数不能为空！', -1);
				}
				if ($Minimum_amount > 10000) {
					error('投资额不能少于10000！', -1);
				}
				$data = array(
					'status' => $status,
					'buy_days' => $buy_days,
					'Settlement_profits' => $Settlement_profits,
					'add_time' => get_datetime(),
					'Minimum_amount' => $Minimum_amount,

				);


				if (!!$this->model->Addsingle_profit($data)) {
					$this->log('新增排单利润成功！');
					success('新增成功！', url('/admin/Scheduling/index'));
				} else {
					$this->log('新增会员失败！');
					error('新增失败！', -1);
				}

			}
		}

		public function mod()
		{
			if (!$id = get('id', 'int')) {
				error('传递的参数值错误！', -1);
			}
			// 单独修改状态
			if (($field = get('field', 'var')) && !is_null($value = get('value', 'var'))) {
				if ($this->model->modProfit($id, "$field='$value'")) {
					alert_back('修改成功');
				} else {
					error('修改失败', -1);
				}
			}
			if ($_POST) {
				$buy_days = post('buy_days');//排单天数
				$status = post('status');//
				$Settlement_profits = post('Settlement_profits');//结算利润
				$Minimum_amount = post('Minimum_amount');//最小投资额
				if ($Settlement_profits == 0) {
					error('结算利润不正确！', -1);
				}
				if ($buy_days == 0) {
					error('排单天数不正确！', -1);
				}
				if (!$buy_days) {
					error('排单天数不能为空！', -1);
				}
				if ($Minimum_amount > 10000) {
					error('投资额不能少于10000！', -1);
				}
				$data = array(
					'status' => $status,
					'buy_days' => $buy_days,
					'Settlement_profits' => $Settlement_profits,
					'add_time' => get_datetime(),
					'Minimum_amount' => $Minimum_amount,

				);
				// 执行修改
				if ($this->model->modProfit($id, $data)) {
					$this->log('修改排单利润成功！');
					if (!!$backurl = get('backurl')) {
						success('修改成功！', base64_decode($backurl));
					} else {
						success('修改成功！', url('/admin/Scheduling/index'));
					}
				} else {
					$this->log('修改会员失败！');
					error('修改失败！', -1);
				}
			} else {
				// 调取修改内容
				$this->assign('mod', true);
				if (!$result = $this->model->getProfit($id)) {
					error('编辑的内容已经不存在！', -1);
				}
				$this->assign('profit', $result);
				$this->display('scheduling/scheduling.html');
			}
		}
	}